﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WXY.Course.BLL;
using WXY.Course.Common.MVCExtension;
using WXY.Course.Model;
using WXY.Course.Web.Areas.Admin.Filter;

namespace WXY.Course.Web.Areas.Admin.Controllers
{
    public class MenuController : Controller
    {
        private MenuBLL _menuBLL = new MenuBLL();

        public ActionResult List(int? page, string name, DateTime? beginTime, DateTime? endTime)
        {
            if (!page.HasValue) page = 1;
            if (endTime.HasValue) endTime = endTime.Value.AddDays(1);

            var list = _menuBLL.GetList();

            return View(list);
        }

        public ActionResult Edit(int? id)
        {
            if (id.HasValue)
            {
                var info = _menuBLL.Get(id.Value);
                return View(info);
            }
            return View();
        }
        [HttpPost]
        public ActionResult Edit(MenuInfo menu)
        {
            if (!ModelState.IsValid)
            {
                return View(menu);
            }

            bool editResult = false;
            if (menu.Code.HasValue)
            {
                editResult = _menuBLL.Update(menu) > 0;
            }
            else
            {
                editResult = _menuBLL.Insert(menu) > 0;
            }

            if (editResult)
            {
                return RedirectToAction("List");
            }
            return View(menu);
        }

        [HttpPost]
        [JsonExceptionFilter(typeof(ResourceController), JsonRequestBehavior.AllowGet)]
        public JsonResult Delete(int id)
        {
            var result = new BoolResult<int>
            {
                IsSuccess = true,
                Data = _menuBLL.Delete(id)
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //public JsonResult Sort(int id, SortType sortType)
        //{
        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}
    }
    //public enum SortType
    //{
    //    RawUp,
    //    RawDown
    //}
}
